//
//  PillowSetViewController.m
//  Lightthings
//
//  Created by 王岩 on 2025/2/13.
//

#import "PillowSetViewController.h"
#import "LevelSetModel.h"

@interface PillowSetViewController ()
/* <#note#> */
@property (nonatomic, strong) NormalArrowModel * switchModel ,* switchModel2 ;
@property (nonatomic, strong) LevelSetModel * levelSetModel;

/* <#note#> */
@property (nonatomic, assign)  BOOL dahanSwith, relaxSwith;

@end

@implementation PillowSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"设置");
    
    self.collectionInsets = kEdge(self.jy_bar_h, 0, 0, 0);

    MainSectionModel * s = [[MainSectionModel alloc] init];
    [s sectionInsets:kEdge(10, 15, 15, 15) ls:10 is:10];
    
    _switchModel = [NormalArrowModel createTitle:SWString(@"睡前放松") size:CGSizeMake((kScreenWidth - 40) / 2, 120) hiddenArrow:YES action:nil];
    _switchModel.idfBlock = ^NSString * _Nonnull{
        return @"PillowSwitchOpenCell";
    };
    _switchModel.value = SWString(@"放松关闭中");
    _switchModel.icon = @"形状 2";
    _switchModel.color = @"#161A40";
    _switchModel.action = kMsgForward(@"relaxSetOpen", WKProxy(self));
    [s.items addObject:_switchModel];

    _switchModel2 = [NormalArrowModel createTitle:SWString(@"减鼾干预") size:CGSizeMake((kScreenWidth - 40) / 2, 120) hiddenArrow:YES action:nil];
    _switchModel2.idfBlock = ^NSString * _Nonnull{
        return @"PillowSwitchOpenCell";
    };
    _switchModel2.value = SWString(@"干预关闭中");
    _switchModel2.icon = @"形状 3";
    _switchModel2.color = @"#7682FF";
    _switchModel2.action = kMsgForward(@"dahanOpen", WKProxy(self));
    [s.items addObject:_switchModel2];

//    _timeSetModel = [[PillowTimeSetModel alloc] init];
//    _timeSetModel.start = @"8:00";
//    _timeSetModel.end = @"20:00";
//    [s.items addObject:_timeSetModel];

    _levelSetModel = [[LevelSetModel alloc] init];
    _levelSetModel.lowerAction = kMsgPackForward(@"levelSet:", WKProxy(self),@1);
    _levelSetModel.middleAction = kMsgPackForward(@"levelSet:", WKProxy(self),@2);
    _levelSetModel.heighAction = kMsgPackForward(@"levelSet:", WKProxy(self),@3);
    _levelSetModel.relaxlowerAction = kMsgPackForward(@"relaxlevelSet:", WKProxy(self),@1);
    _levelSetModel.relaxmiddleAction = kMsgPackForward(@"relaxlevelSet:", WKProxy(self),@2);
    _levelSetModel.relaxheighAction = kMsgPackForward(@"relaxlevelSet:", WKProxy(self),@3);

    [s.items addObject:_levelSetModel];
    
    self.dataSources= @[s];
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    [self readData];
    // Do any additional setup after loading the view.
}

//读取放松设置
- (void)readData {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_GetMassage ToPeripheral:self.peripheral Value:@[] reBack:^(NSString * _Nonnull data) {
        [self updateRelaxInfo:data];
        [self getDaHanOpen];
    }];
}
//获取打鼾开关
- (void)getDaHanOpen {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_ReadDate ToPeripheral:self.peripheral Value:@[] reBack:^(NSString * _Nonnull back) {
        [self updateDaHanOpenInfo:back];
        [self  getDaHanLevel];
    }];
}
//获取打鼾等级
- (void)getDaHanLevel {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_GradeSnoring ToPeripheral:self.peripheral Value:@[] reBack:^(NSString * _Nonnull back) {
        [self updateDaHanLevelInfo:back];
    }];
}
//设置打鼾等级
- (void)levelSet:(NSNumber *)number {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_SnoreInterventionIntensity ToPeripheral:self.peripheral Value:@[ number, @0, @0, @0] reBack:^(NSString * _Nonnull back) {
        [self updateDaHanLevelInfo:back];
    }];
}
//设置放松等级
- (void)relaxlevelSet:(NSNumber *)number {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_RelaxSet ToPeripheral:self.peripheral Value:@[@(self.relaxSwith), @15, number] reBack:^(NSString * _Nonnull back) {
        [self updateRelaxInfo:back];
    }];
}
//设置放松开关
- (void)relaxSetOpen {
    [PillowCommandTool.sharedInstance writeStormCommand:Pillow_RelaxSet ToPeripheral:self.peripheral Value:@[@(!self.relaxSwith), @15, @(self.levelSetModel.relaxLevel)] reBack:^(NSString * _Nonnull back) {
        [self updateRelaxInfo:back];
    }];
}
//设置打鼾开关
- (void)dahanOpen {
    [PillowCommandTool.sharedInstance updateDateAndTimeCallBack:^(NSString * _Nonnull data) {
        [self updateDaHanOpenInfo:data];
    } RealTimeData:NO StopSwith:!self.dahanSwith ToPeripheral:self.peripheral];
}
//更新UI放松信息
- (void)updateRelaxInfo:(NSString *)back {
    if ([[back substringWithRange:NSMakeRange(4, 2)] isEqualToString:@"b3"]) {
        NSString * sleepdata = [back substringFromIndex:8];
        sleepdata  = [sleepdata substringToIndex:sleepdata.length - 4];
        
        NSString * open = [sleepdata substringWithRange:NSMakeRange(0, 2)];
        long openValue = strtoll([[NSString stringWithFormat:@"0x%@",open] UTF8String], 0, 16);
        self.relaxSwith = openValue == 1;
        if (openValue == 0) {
            self.switchModel.color = @"#999999";
            self.switchModel.value = SWString(@"放松关闭中");
        }else {
            self.switchModel.color = @"#161A40";
            self.switchModel.value = SWString(@"放松开启中");
        }
        
        NSString * time = [sleepdata substringWithRange:NSMakeRange(2, 2)];
        long timeValue = strtoll([[NSString stringWithFormat:@"0x%@",time] UTF8String], 0, 16);

        NSString * level = [sleepdata substringWithRange:NSMakeRange(4, 2)];
        long levelValue = strtoll([[NSString stringWithFormat:@"0x%@",level] UTF8String], 0, 16);
        
        self.levelSetModel.relaxLevel = levelValue;
        
        [self reloadIfNeeded];
       
    }
}
//更新UI打鼾等级信息
- (void)updateDaHanLevelInfo:(NSString *)back {
    if ([[back substringWithRange:NSMakeRange(4, 2)] isEqualToString:@"b5"]) {

        NSString * open = [back substringWithRange:NSMakeRange(8, 2)];
        long openValue = strtoll([[NSString stringWithFormat:@"0x%@",open] UTF8String], 0, 16);

        self.levelSetModel.level = openValue;
        [self reloadIfNeeded];
    }
}

- (void)updateDaHanOpenInfo:(NSString *)back {
    if ([[back substringWithRange:NSMakeRange(4, 2)] isEqualToString:@"c0"]) {

        NSString * open = [back substringWithRange:NSMakeRange(18, 2)];
        long openValue = strtoll([[NSString stringWithFormat:@"0x%@",open] UTF8String], 0, 16);
        self.dahanSwith = openValue== 1;
        if (openValue == 0) {
            self.switchModel2.color = @"#999999";
            self.switchModel2.value = SWString(@"干预关闭中");
        }else {
            self.switchModel2.color = @"#161A40";
            self.switchModel2.value = SWString(@"干预开启中");
        }
        
        [self reloadIfNeeded];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
